package com.zm.demo.javaagent.attach;

import com.sun.tools.attach.VirtualMachine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Optional;

public class AgentLoader {
    private static Logger logger = LoggerFactory.getLogger(AgentLoader.class);


    public void run(String jvmPid, String targetClassName, String jarPath ) {
        try {
            logger.info("Attaching to target JVM with PID: " + jvmPid);
            VirtualMachine jvm = VirtualMachine.attach(jvmPid);
            jvm.loadAgent(jarPath, targetClassName);
            jvm.detach();
            logger.info("Attached to target JVM and loaded Java agent successfully");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
}